const Graph = require("../template")

function testGraphComplexScenarios() {
  console.log(
    "1. 复杂图结构测试:",
    (() => {
      const graph = new Graph()
      
      // 构建复杂图结构
      const vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
      const edges = [
        ['A', 'B'], ['A', 'C'], ['B', 'D'], ['C', 'E'],
        ['D', 'F'], ['E', 'F'], ['F', 'G'], ['G', 'H'],
        ['A', 'F'], ['B', 'E'], ['C', 'D']
      ]
      
      vertices.forEach(v => graph.addVertex(v))
      edges.forEach(([from, to]) => graph.addEdge(from, to))
      
      // 验证图结构
      const vertexCount = graph.getVertexCount()
      const edgeCount = graph.getEdgeCount()
      const hasComplexEdge = graph.hasEdge('A', 'F')
      
      return vertexCount === 8 &&
             edgeCount === 11 &&
             hasComplexEdge === true
    })()
  )

  console.log(
    "2. 大量数据管理测试:",
    (() => {
      const graph = new Graph()
      const count = 1000
      
      // 插入大量顶点
      for (let i = 0; i < count; i++) {
        graph.addVertex(`V${i}`)
      }
      
      // 添加边形成网格结构
      for (let i = 0; i < count - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      // 验证图结构
      const vertexCount = graph.getVertexCount()
      const edgeCount = graph.getEdgeCount()
      
      return vertexCount === count &&
             edgeCount === count - 1
    })()
  )

  console.log(
    "3. 动态图操作测试:",
    (() => {
      const graph = new Graph()
      
      // 阶段1：构建初始图
      const vertices = ['A', 'B', 'C', 'D', 'E']
      const edges = [['A', 'B'], ['B', 'C'], ['C', 'D'], ['D', 'E']]
      
      vertices.forEach(v => graph.addVertex(v))
      edges.forEach(([from, to]) => graph.addEdge(from, to))
      
      const initialVertexCount = graph.getVertexCount()
      const initialEdgeCount = graph.getEdgeCount()
      
      // 阶段2：添加更多顶点和边
      graph.addVertex('F')
      graph.addVertex('G')
      graph.addEdge('E', 'F')
      graph.addEdge('F', 'G')
      graph.addEdge('A', 'G') // 创建环
      
      const midVertexCount = graph.getVertexCount()
      const midEdgeCount = graph.getEdgeCount()
      
      // 阶段3：删除部分顶点和边
      graph.removeVertex('C')
      graph.removeEdge('A', 'G')
      
      const finalVertexCount = graph.getVertexCount()
      const finalEdgeCount = graph.getEdgeCount()
      
      return initialVertexCount === 5 &&
             initialEdgeCount === 4 &&
             midVertexCount === 7 &&
             midEdgeCount === 7 &&
             finalVertexCount === 6 &&
             finalEdgeCount === 5
    })()
  )

  console.log(
    "4. 并发操作模拟测试:",
    (() => {
      const graph = new Graph()
      
      // 模拟多个操作同时进行
      const operations = []
      
      // 操作1：批量添加顶点
      for (let i = 0; i < 50; i++) {
        operations.push(() => graph.addVertex(`V${i}`))
      }
      
      // 操作2：批量添加边
      for (let i = 0; i < 49; i++) {
        operations.push(() => graph.addEdge(`V${i}`, `V${i + 1}`))
      }
      
      // 操作3：批量查询
      for (let i = 0; i < 50; i++) {
        operations.push(() => graph.getNeighbors(`V${i}`))
      }
      
      // 随机执行操作
      const shuffledOps = operations.sort(() => Math.random() - 0.5)
      shuffledOps.forEach(op => op())
      
      // 验证最终状态
      const finalVertexCount = graph.getVertexCount()
      const finalEdgeCount = graph.getEdgeCount()
      
      return finalVertexCount === 50 &&
             finalEdgeCount === 49
    })()
  )

  console.log(
    "5. 边界条件处理测试:",
    (() => {
      const graph = new Graph()
      
      // 测试边界条件
      graph.addVertex('A')
      graph.addVertex('B')
      graph.addEdge('A', 'B')
      
      // 测试删除不存在的顶点
      graph.removeVertex('C')
      
      // 测试删除不存在的边
      graph.removeEdge('A', 'C')
      graph.removeEdge('C', 'A')
      
      // 测试查询不存在的顶点
      const neighborsC = graph.getNeighbors('C')
      const hasEdgeAC = graph.hasEdge('A', 'C')
      
      // 验证状态
      const vertexCount = graph.getVertexCount()
      const edgeCount = graph.getEdgeCount()
      const hasEdgeAB = graph.hasEdge('A', 'B')
      
      return vertexCount === 2 &&
             edgeCount === 1 &&
             hasEdgeAB === true &&
             hasEdgeAC === false &&
             neighborsC.length === 0
    })()
  )

  console.log(
    "6. 错误恢复测试:",
    (() => {
      const graph = new Graph()
      
      // 正常操作
      graph.addVertex('A')
      graph.addVertex('B')
      graph.addEdge('A', 'B')
      
      // 尝试一些边界操作
      graph.addVertex('A') // 重复添加
      graph.addEdge('A', 'B') // 重复添加边
      graph.addEdge('C', 'D') // 添加不存在的顶点
      
      // 验证状态
      const vertexCount = graph.getVertexCount()
      const edgeCount = graph.getEdgeCount()
      const hasEdgeAB = graph.hasEdge('A', 'B')
      const hasEdgeCD = graph.hasEdge('C', 'D')
      
      return vertexCount === 3 && // A, B, C, D
             edgeCount === 2 && // A-B, C-D
             hasEdgeAB === true &&
             hasEdgeCD === true
    })()
  )

  console.log(
    "7. 内存效率测试:",
    (() => {
      const graph = new Graph()
      
      // 添加大量数据测试内存效率
      const startMemory = process.memoryUsage ? process.memoryUsage().heapUsed : 0
      
      for (let i = 0; i < 1000; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < 999; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const endMemory = process.memoryUsage ? process.memoryUsage().heapUsed : 0
      const memoryUsed = endMemory - startMemory
      
      // 验证统计信息
      const vertexCount = graph.getVertexCount()
      const edgeCount = graph.getEdgeCount()
      
      return vertexCount === 1000 &&
             edgeCount === 999 &&
             memoryUsed < 100 * 1024 * 1024 // 内存使用应该合理
    })()
  )

  console.log(
    "8. 图遍历复杂测试:",
    (() => {
      const graph = new Graph()
      
      // 构建复杂图
      const vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
      const edges = [
        ['A', 'B'], ['A', 'C'], ['B', 'D'], ['C', 'E'],
        ['D', 'F'], ['E', 'F'], ['F', 'G'], ['G', 'H'],
        ['A', 'F'], ['B', 'E']
      ]
      
      vertices.forEach(v => graph.addVertex(v))
      edges.forEach(([from, to]) => graph.addEdge(from, to))
      
      // DFS遍历
      let dfsCount = 0
      graph.dfs('A', () => dfsCount++)
      
      // BFS遍历
      let bfsCount = 0
      graph.bfs('A', () => bfsCount++)
      
      // 路径查找
      const path = graph.shortestPath('A', 'H')
      
      return dfsCount === 8 &&
             bfsCount === 8 &&
             path.length > 0 &&
             path[0] === 'A' &&
             path[path.length - 1] === 'H'
    })()
  )

  console.log(
    "9. this上下文复杂场景测试:",
    (() => {
      const complexObj = {
        multiplier: 3,
        processComplex: function(vertices, edges) {
          const graph = new Graph()
          vertices.forEach(v => graph.addVertex(v))
          edges.forEach(([from, to]) => graph.addEdge(from, to))
          
          let count = 0
          graph.dfs(vertices[0], () => count++)
          return count * this.multiplier
        }
      }
      
      const vertices = ['A', 'B', 'C', 'D']
      const edges = [['A', 'B'], ['B', 'C'], ['C', 'D']]
      const result = complexObj.processComplex(vertices, edges)
      
      return result === 12 // 4 * 3 = 12
    })()
  )

  console.log(
    "10. 复杂对象this绑定场景测试:",
    (() => {
      const complexConfigObj = {
        config: { 
          threshold: 3, 
          maxVertices: 10 
        },
        processWithComplexConfig: function(vertices, edges) {
          const graph = new Graph()
          vertices.forEach(v => graph.addVertex(v))
          edges.forEach(([from, to]) => graph.addEdge(from, to))
          
          const vertexCount = graph.getVertexCount()
          const edgeCount = graph.getEdgeCount()
          
          return vertexCount >= this.config.threshold &&
                 vertexCount <= this.config.maxVertices &&
                 edgeCount > 0
        }
      }
      
      const vertices = ['A', 'B', 'C', 'D', 'E']
      const edges = [['A', 'B'], ['B', 'C'], ['C', 'D'], ['D', 'E']]
      return complexConfigObj.processWithComplexConfig(vertices, edges) === true
    })()
  )

  console.log(
    "11. 极端负载测试:",
    (() => {
      const graph = new Graph()
      
      // 在极端条件下添加大量数据
      for (let i = 0; i < 2000; i++) {
        graph.addVertex(`V${i}`)
      }
      
      // 添加边形成复杂结构
      for (let i = 0; i < 2000; i += 2) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      // 验证统计信息
      const vertexCount = graph.getVertexCount()
      const edgeCount = graph.getEdgeCount()
      
      // 测试遍历性能
      let dfsCount = 0
      graph.dfs('V0', () => dfsCount++)
      
      return vertexCount === 2000 &&
             edgeCount === 1000 &&
             dfsCount > 0
    })()
  )

  console.log(
    "12. 综合复杂场景测试:",
    (() => {
      const graph = new Graph()
      
      // 综合测试：多种操作混合
      const operations = []
      
      // 插入各种数据
      for (let i = 0; i < 100; i++) {
        operations.push(() => graph.addVertex(`V${i}`))
        operations.push(() => graph.addVertex(`U${i}`))
      }
      
      // 添加边
      for (let i = 0; i < 100; i++) {
        operations.push(() => graph.addEdge(`V${i}`, `U${i}`))
        if (i < 99) {
          operations.push(() => graph.addEdge(`V${i}`, `V${i + 1}`))
        }
      }
      
      // 执行操作
      operations.forEach(op => op())
      
      // 删除部分数据
      for (let i = 0; i < 50; i++) {
        graph.removeVertex(`U${i}`)
      }
      
      // 验证最终状态
      const finalStats = {
        vertexCount: graph.getVertexCount(),
        edgeCount: graph.getEdgeCount(),
        hasEdge: graph.hasEdge('V0', 'V1'),
        neighbors: graph.getNeighbors('V0').length
      }
      
      // 验证所有统计信息都合理
      return finalStats.vertexCount === 150 && // 200 - 50 = 150
             finalStats.edgeCount > 0 &&
             finalStats.hasEdge === true &&
             finalStats.neighbors > 0 &&
             typeof finalStats.vertexCount === 'number' &&
             typeof finalStats.edgeCount === 'number' &&
             typeof finalStats.hasEdge === 'boolean' &&
             typeof finalStats.neighbors === 'number'
    })()
  )
}

testGraphComplexScenarios()
